/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 * Vulkan API Profiling Wrapper - 自动插桩所有 Vulkan API 调用
 */

#ifndef VULKAN_PROFILING_WRAPPER_HPP
#define VULKAN_PROFILING_WRAPPER_HPP

#include <vulkan/vulkan.h>
#include "PerformanceProfiler.hpp"

/**
 * Vulkan API 包装宏 - 自动测量API调用时间
 * 使用方法: 
 *   PROFILE_VK_CALL(vkQueueSubmit(queue, submitCount, pSubmits, fence));
 */
#define PROFILE_VK_CALL(vkCall) \
    do { \
        PROFILE_API_CALL(#vkCall); \
        vkCall; \
    } while(0)

/**
 * 带返回值的 Vulkan API 包装宏
 * 使用方法:
 *   VkResult result = PROFILE_VK_CALL_RET(vkCreateDevice(...));
 */
#define PROFILE_VK_CALL_RET(vkCall) \
    [&]() { \
        PROFILE_API_CALL(#vkCall); \
        return vkCall; \
    }()

/**
 * Vulkan 渲染管线性能分析器
 * 用于测量完整渲染管线各阶段的性能
 */
class VulkanPipelineProfiler {
public:
    // 管线阶段枚举
    enum class PipelineStage {
        FrameStart,
        AcquireImage,
        CommandBufferRecord,
        RenderPassBegin,
        DrawCalls,
        RenderPassEnd,
        CommandBufferEnd,
        QueueSubmit,
        QueuePresent,
        FrameEnd
    };

    static void BeginStage(PipelineStage stage) {
        if (!PerformanceProfiler::Instance().IsEnabled()) return;
        PerformanceProfiler::Instance().BeginApiCall(GetStageName(stage));
    }

    static void EndStage(PipelineStage stage) {
        if (!PerformanceProfiler::Instance().IsEnabled()) return;
        PerformanceProfiler::Instance().EndApiCall(GetStageName(stage));
    }

    static void NextFrame() {
        if (!PerformanceProfiler::Instance().IsEnabled()) return;
        PerformanceProfiler::Instance().NextFrame();
    }

private:
    static const char* GetStageName(PipelineStage stage) {
        switch (stage) {
            case PipelineStage::FrameStart: return "Pipeline::FrameStart";
            case PipelineStage::AcquireImage: return "Pipeline::AcquireImage";
            case PipelineStage::CommandBufferRecord: return "Pipeline::CommandBufferRecord";
            case PipelineStage::RenderPassBegin: return "Pipeline::RenderPassBegin";
            case PipelineStage::DrawCalls: return "Pipeline::DrawCalls";
            case PipelineStage::RenderPassEnd: return "Pipeline::RenderPassEnd";
            case PipelineStage::CommandBufferEnd: return "Pipeline::CommandBufferEnd";
            case PipelineStage::QueueSubmit: return "Pipeline::QueueSubmit";
            case PipelineStage::QueuePresent: return "Pipeline::QueuePresent";
            case PipelineStage::FrameEnd: return "Pipeline::FrameEnd";
            default: return "Pipeline::Unknown";
        }
    }
};

// RAII 风格的管线阶段测量器
class ScopedPipelineStageProfiler {
public:
    explicit ScopedPipelineStageProfiler(VulkanPipelineProfiler::PipelineStage stage) 
        : m_stage(stage) {
        VulkanPipelineProfiler::BeginStage(m_stage);
    }
    
    ~ScopedPipelineStageProfiler() {
        VulkanPipelineProfiler::EndStage(m_stage);
    }
    
private:
    VulkanPipelineProfiler::PipelineStage m_stage;
};

// 便捷宏定义
#define PROFILE_PIPELINE_STAGE(stage) \
    ScopedPipelineStageProfiler _pipeline_profiler_##__LINE__(VulkanPipelineProfiler::PipelineStage::stage)

#endif // VULKAN_PROFILING_WRAPPER_HPP
